package cetcbigdata.da.legal_person_service.shanghai;

import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class ShUtil {

    /**
     * 前言信息
     * @param jxDocument
     * @return
     */
    public static JSONObject geExtraInfo(JXDocument jxDocument){
        try {
            JSONObject info = new JSONObject();
            List<Object> data = jxDocument.sel("//div[@class='jbxxContent']/ul//li");
            for (int i = 0, len = data.size(); i < len - 1; i += 2) {
                String key = ((Element) data.get(i)).text();
                String value = ((Element) data.get(i + 1)).text();
                info.put(key, value);
            }
            return info;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 基本信息
     * @param jxDocument
     * @return
     */
    public static JSONObject getBaseInfo(JXDocument jxDocument){
        try {
            JSONObject base = new JSONObject();
            List<JSONObject> baseList = new ArrayList<>();
            List<Object> rs = jxDocument.sel("//div[@class='jbxxContent']//a[@class='barrierFreeA']//ul");
            for (Object o : rs) {
                Elements li = ((Element) o).children();
                for (int i = 0, len = li.size(); i < len;) {
                    JSONObject bascInfo = new JSONObject();
                    String key = li.get(i).text();
                    String vaule = li.get(i+1).text();
                    bascInfo.put(key, vaule);
                    baseList.add(bascInfo);
                    i+=2;
                }
            }
            base.put("基础信息", baseList);
            return base;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 受理条件
     * @param jxDocument
     * @return
     */
    public static JSONObject getAcceptanceConditions(JXDocument jxDocument){
        try {
            JSONObject acceptance = new JSONObject();
            List<Object> rs = jxDocument.sel("//div[@class='sltjContent']//p");
            for (Object r : rs) {
                String sltj = ((Element) r).children().text();
                acceptance.put("受理条件", sltj);
            }
            return acceptance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 申请材料
     * @param jxDocument
     * @return
     */
    public static List<JSONObject> getApplicationMaterialsInfo(JXDocument jxDocument){
        try {
            List<JSONObject> applicationMaterialList = new ArrayList<>();
            List<Object> rs = jxDocument.sel("//div[@class='sqclContent']");
            for (Object r : rs) {
                if (((Element) r).children().size() > 0){
                    JSONObject item = new JSONObject();
                    item.put("填报须知", ((Element) r).child(1).text());
                    item.put("形式标准", ((Element) r).child(3).child(0).text());
                    item.put("申请文书名称", ((Element) r).child(9).child(0).text());
                    applicationMaterialList.add(item);
                }
            }
            List<Object> sel = jxDocument.sel("//div[@class='sqclContent']//table[@class='table table-bordered work-table-0 table-hover']//tr");
            for (int i = 0 , len = sel.size(); i < len - 1; i++) {
                if (i == 0) continue;
                JSONObject items = new JSONObject();
                items.put("材料名称",     ((Element) sel.get(i)).child(0).text());
                items.put("来源渠道",     ((Element) sel.get(i)).child(1).child(0).text());
                items.put("来源渠道说明", ((Element) sel.get(i)).child(2).child(0).text());
                items.put("材料类型",     ((Element) sel.get(i)).child(3).child(0).text());
                items.put("纸质材料份数", ((Element) sel.get(i)).child(4).child(0).text());
                items.put("材料形式",     ((Element) sel.get(i)).child(5).child(0).text());
                items.put("材料必要性",   ((Element) sel.get(i)).child(6).child(0).text());

                String remark = ((Element) sel.get(i)).child(7).children().size() > 0 ?
                        ((Element) sel.get(i)).child(7).child(0).attr("data-content") : "";
                items.put("备注", remark) ;

                applicationMaterialList.add(items);

            }
            return applicationMaterialList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 办理流程
     * @param jxDocument
     * @return
     */
    public static JSONObject getProcess(JXDocument jxDocument){
        JSONObject process = new JSONObject();

        //获取流程图
        List<Object> rs = jxDocument.sel("//div[@class='bllcContent']");
        List<JSONObject> processList = new ArrayList<>();
        for (Object r : rs) {
            JSONObject item = new JSONObject();
            String url = ((Element) r).child(0).attr("href").replace("/govPortals/revision/bszn/bsfw_img.html?img=", "");
            item.put("查看流程图", url);
            processList.add(item);
        }

        //获取特别程序
        List<Object> data = jxDocument.sel("//div[@class='bllcContent']//p");
        for (int i = 0, len = data.size(); i < len - 1;) {
            JSONObject items = new JSONObject();
            String key = ((Element) data.get(i)).children().text();
            String value = ((Element) data.get(i + 1)).children().text();
            i += 2;
            items.put(key, value);
            processList.add(items);
        }

        //获取办理步骤
        List<Object> sel = jxDocument.sel("//div[@class='bllcTables']//div[@class='lctableBox ']//table[@class='table table-bordered work-table-0 table-hover']//tbody//tr | //div[@class='bllcTables']//div[@class='lctableBox bottom']//table[@class='table table-bordered work-table-0 table-hover']//tbody//tr");
        for (Object s : sel) {
            JSONObject selItems = new JSONObject();
            selItems.put("办理步骤",           ((Element) s).child(0).text());
            selItems.put("办理时限（工作日）", ((Element) s).child(1).text());
            selItems.put("办理人员",           ((Element) s).child(2).text());
            selItems.put("审查标准",           ((Element) s).child(3).text());
            processList.add(selItems);
        }
        process.put("办理流程", processList);

        return process;
    }

    /**
     * 办理地点/时间
     * @param jxDocument
     * @return
     */
    public static JSONObject getPlaceOrTime(JXDocument jxDocument){
        try {
            JSONObject placeOrTime = new JSONObject();
            List<Object> rs = jxDocument.sel("//div[@class='pull-left']//p");
            for (int i = 0; i < rs.size();) {
                String key = ((Element) rs.get(i)).child(0).text();
                String value = ((Element) rs.get(i+1)).child(0).text();
                i += 2;
                placeOrTime.put(key, value);
            }
            return placeOrTime;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 常见问题
     * @param jxDocument
     * @return
     */
    public static JSONObject getProblemInfo(JXDocument jxDocument){
        try {
            JSONObject problemInfo = new JSONObject();
            List<Object> rs = jxDocument.sel("//div[@class='cjwtContent']//p");
            List<JSONObject> problemList = new ArrayList<>();
            if (rs.size() == 2){
                for (int i = 0; i < rs.size();) {
                    JSONObject items = new JSONObject();
                    String key = ((Element) rs.get(i)).children().text();
                    String value = ((Element) rs.get(i + 1)).children().text();
                    i += 2;
                    items.put(key, value);
                    problemList.add(items);
                }
            }else if (rs.size() > 2){
                for (int i = 1; i < rs.size();) {
                    JSONObject items = new JSONObject();
                    String key = ((Element) rs.get(i)).children().text();
                    String value = ((Element) rs.get(i + 1)).children().text();
                    i += 2;
                    items.put(key, value);
                    problemList.add(items);
                }
            }
            problemInfo.put("常见问题", problemList);
            return problemInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 服务依据
     * @param jxDocument
     * @return
     */
    public static JSONObject getServiceBasis(JXDocument jxDocument){
        JSONObject serviceBasis = new JSONObject();
        List<Object> rs = jxDocument.sel("//div[@class='slyjContent']");
        String basis = null;
        for (Object r : rs) {
            basis = ((Element) r).child(0).child(0).text();
        }
        serviceBasis.put("服务依据", basis);
        return serviceBasis;
    }

    /**
     * 服务收费
     * @param jxDocument
     * @return
     */
    public static List<JSONObject> getServiceCharge(JXDocument jxDocument){
        int index = 2;
        List<JSONObject> chargeList = new ArrayList<>();
        List<Object> rs = jxDocument.sel("//div[@class='qlywContent']");
        for (Object r : rs) {
            JSONObject item = new JSONObject();
            String[] chagre = ((Element) r).child(0).text().split("：");
           if(chagre.length > index){
               String key = chagre[0];
               String value = chagre[1];
               item.put(key, value);
               chargeList.add(item);
           }
        }

        //收费项目信息
        List<Object> data = jxDocument.sel("//div[@class='qlywContent']//table[@class='table table-bordered work-table-0 table-hover']//tr");
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) continue;
            JSONObject items = new JSONObject();
            items.put("收费项目名称", ((Element) data.get(i)).child(0).child(0).text());
            items.put("收费环节",     ((Element) data.get(i)).child(1).child(0).text());
            items.put("收费标准",     ((Element) data.get(i)).child(2).child(0).text());
            items.put("收费依据",     ((Element) data.get(i)).child(3).child(0).text());
            items.put("是否允许减免", ((Element) data.get(i)).child(4).child(0).text());
            items.put("减免说明",     ((Element) data.get(i)).child(5).child(0).text());
            items.put("允许减免依据", ((Element) data.get(i)).child(6).child(0).text());
            items.put("备注",         ((Element) data.get(i)).child(7).child(0).text());
            chargeList.add(items);
        }
        return chargeList;
    }

}
